<?php
if(!defined('IN_MANU')) { die('[ERROR] You cannot load this page directly !!!'); }

class ConfigManager
{
	//private $db = null;
	public $host        = '';
    public $username    = '';
    public $pwd         = '';
    public $dbName      = '';
    public $dbPrefix    = '';

// 	minify CSS & JS
// 	public $minify = false;
    
    private function checkDeloyMode()
    {
        $prefixURL = $_SERVER['HTTP_HOST'];
        $checkURL = strpos($prefixURL, 'demo3');
        
        if($checkURL === false){
            return false;
        }
        else {
            return true;
        }
    }
    
	function __construct($db)
	{
        $deloyMode = $this->checkDeloyMode();
		$this->db = $db;
        if(!$deloyMode){// localhost
            // init config
            $this->host        = 'localhost';
            $this->username    = 'root';
            $this->pwd         = '';
            $this->dbName      = 'book store';
            $this->dbPrefix    = '';
        }
        else {// demo3
            // init config
            $this->host        = 'localhost';
            $this->username    = 'u_intershipphp2';
            $this->pwd         = '(IJNbhu8';
            $this->dbName      = 'intershipphp2';
            $this->dbPrefix    = '';
        } 
        
	}

	public function getById($id)
	{
		$query = "SELECT * FROM configuration WHERE id = $id";
		$result = $this->db->get_data($query, 'config');
		return $result[0];
	}

	public function getByKey($key) 
	{
		$query = "SELECT * 
				  FROM configuration AS c 
				  WHERE c.key = " . var_export($key, true);
		//echo $query;		
		$result = $this->db->get_data($query, 'config');
		//echo "class = ".get_class($result[0]);
		return $result[0];
	}
	
	public function getConfigLimit($fst, $num)
	{
		$query = "SELECT c.id, c.key, c.value FROM configuration as c LIMIT $fst, $num";
		$tblResult = $this->db->getMixedData($query);
		return $tblResult;
	}

	public function getConfigAll()
	{

		$query = "SELECT c.id, c.key, c.value FROM configuration as c ";
		 
		//$this->db->startConnection();
		$tblResult = $this->db->getMixedData($query);
		//$this->db->closeConnection();
		 
		return $tblResult;
	}

	public function countConfigById()
	{
		if ($this->db != null) {
			//$this->db->startConnection();

			$result = $this->db->dbQuery("SELECT COUNT(id) AS num FROM configuration"); 		  
		  
			$row = mysql_fetch_assoc($result);
			$num = $row['num'];
			//$this->db->closeConnection();
			return $num;
		}
	}
    
    public function setDelConfigById($id)
    {
        $query = "DELETE FROM configuration WHERE id = $id";
        $this->db->dbQuery($query);
    }
    
    public function addConfig($key, $value)
    {
        $query = "INSERT INTO configuration(configuration.key, configuration.value) VALUE(" . var_export($key, true) .", $value)";
        $this->db->dbQuery($query);
    }
    public function updateConfig($id, $value)
    {    	    	
        $query = "UPDATE configuration 
        		  SET configuration.value = " . var_export($value, true) . 
        		  "WHERE configuration.id = $id";
        //echo $query;        
        $this->db->dbQuery($query);
    }
    
    public function getConfigById($id)
	{
		  
    	$query = "SELECT c.key, c.value FROM configuration as c WHERE id = $id";
    	$tblResult = $this->db->getMixedData($query);
    	return $tblResult[0];
	}
}
?>